package com.tuhu.server.util;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

    public static boolean isEmpty(CharSequence source) {
        return source == null || source.length() == 0;
    }

    public static boolean isNotEmpty(CharSequence source) {
        return !isEmpty(source);
    }


    /**
     * @param source 格式形式:  18,12,7
     *               允许空格，空格会优先删除
     * @return
     */
    public static List<Integer> parseToList(String source) {
        if (source == null || source.length() == 0) {
            return null;
        }

        List<Integer> resultList = new ArrayList<>();
        String[] array = source.replace(" ", "").split(",");

        for (int i = 0; i < array.length; i++) {
            resultList.add(Integer.parseInt(array[i]));
        }

        return resultList;
    }

    public static int[] parseToIntArray(String source) {
        if (source == null || source.length() == 0) {
            return null;
        }

        String[] array = source
                .replace("[", "")
                .replace("]", "")
                .replace(" ", "")
                .split(",");
        int[] intArr = new int[array.length];

        for (int i = 0; i < intArr.length; i++) {
            intArr[i] = Integer.parseInt(array[i]);
        }

        return intArr;
    }

    public static double[] parseToDoubleArray(String source) {
        if (source == null || source.length() == 0) {
            return null;
        }

        String[] array = source
                .replace("[", "")
                .replace("]", "")
                .replace(" ", "")
                .split(",");
        double[] intArr = new double[array.length];

        for (int i = 0; i < intArr.length; i++) {
            intArr[i] = Double.parseDouble(array[i]);
        }

        return intArr;
    }

    /**
     * 判断是否为空白字符串
     * 以下认为是空白字符：空格符、换行符、换页符、制表符、段落分隔符
     *
     * @param str
     * @return
     */
    public static boolean isBlank(String str) {
        int strLen;
        if (str != null && (strLen = str.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(str.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static int length(CharSequence source) {
        return source == null ? 0 : source.length();
    }

    /**
     * 取源字符串的前 N 字符串，源串不足长度时取整个源串
     */
    public static String take(String source, int length) {
        if (isEmpty(source)) {
            return null;
        }
        if (length >= source.length()) {
            return source;
        } else {
            return source.substring(0, length);
        }
    }

    public static String getFirst(CharSequence source) {
        if (isEmpty(source)) {
            return null;
        }
        return source.subSequence(0, 1).toString();
    }

    public static CharSequence getLast(CharSequence source) {
        if (isEmpty(source)) {
            return null;
        }
        return source.subSequence(source.length() - 1, source.length());
    }

    public static String subSequence(String source, int startIndex, int endIndex) {
        if (isEmpty(source)) {
            return null;
        }
        return source.substring(startIndex, endIndex);
    }

    /**
     * 判断两个字符串的内容是否相同<p>
     * 注：<p>
     * 两个null认为是不相同 <p>
     * 如果需要两个null认为是相同，请使用{@link java.util.Objects#equals(Object, Object)}，
     *
     * @return true:相同；false:不相同
     */
    public static boolean equals(String strParam1, String strParam2) {
        if (strParam1 == null || strParam2 == null) {
            return false;
        }
        return strParam1.equals(strParam2);
    }

    public static boolean equalsIgnoreCase(String strParam1, String strParam2) {
        if (strParam1 == null || strParam2 == null) {
            return false;
        }
        return strParam1.equalsIgnoreCase(strParam2);
    }

    public static int compare(String strParam1, String strParam2) {
        if (strParam1 == null || strParam2 == null) {
            return -1;
        }
        return strParam1.compareTo(strParam2);
    }


    public static String filter(String source, String... target) {
        if (source == null || target == null) {
            return null;
        }
        if (target.length == 0) {
            return source;
        }
        for (int i = 0; i < target.length; i++) {
            source = source.replaceAll(target[i], "");
        }

        return source;
    }

    public static boolean contains(String source, String target) {
        if (source == null || target == null) {
            return false;
        }
        return source.contains(target);
    }

    public static boolean containsIgnoreCase(String source, String target) {
        if (source == null || target == null) {
            return false;
        }
        return source.toLowerCase().contains(target.toLowerCase());
    }

    /**
     * 判断 字符串数组中 是否存在 与 目标字符串 内容相同的 字符串
     * <p>
     * 注：比较的是字符串的内容，而不是内存地址
     *
     * @return
     */
    public static boolean contains(String[] array, String target) {
        Comparator comparator = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if (o1 == null || o2 == null) {
                    return -1;
                }
                return o1.equals(o2) ? 0 : o1.compareTo(o2);
            }
        };
        return CollectionUtils.contains(array, target, comparator);
    }

    /**
     * 判断 字符串列表中 是否存在 与 目标字符串 内容相同的 字符串
     * <p>
     * 注：比较的是字符串的内容，而不是内存地址
     *
     * @return
     */
    public static boolean contains(List<String> stringList, String target) {
        Comparator comparator = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if (o1 == null || o2 == null) {
                    return -1;
                }
                return o1.equals(o2) ? 0 : o1.compareTo(o2);
            }
        };
        return CollectionUtils.contains(stringList, target, comparator);
    }

    /**
     * 检查 字符串 是否 至少含有一个有效字符
     * 注：空格、换行、制表符 为无效字符
     */
    public static boolean hasValidWord(String source) {
        if (isEmpty(source)) {
            return false;
        }

        String dest;
        Pattern p = Pattern.compile("\\s*|\t|\r|\n");
        Matcher m = p.matcher(source);
        dest = m.replaceAll("");

        return isNotEmpty(dest);
    }

    public static List<String> delRepeatStringList(List<String> sourceList) {
        if (sourceList == null) {
            return null;
        }

        List<String> resultList = new ArrayList<>();
        for (int i = 0; i < sourceList.size(); i++) {
            String str = sourceList.get(i);
            if (!resultList.contains(str)) {
                resultList.add(str);
            }
        }

        return resultList;
    }

    public static String randomSelect1From(String str) {
        String[] a = str.replace(" ", "").split(",");

        int index = (int) (Math.random() * a.length);

        return a[index];
    }

    public static String randomSortFrom(String str) {
        String[] arr = str.replace(" ", "").split(",");

        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {

            int index = random.nextInt(arr.length);
            //temp：临时变量，存储index位置处的值
            String temp = arr[index];
            arr[index] = arr[i];
            arr[i] = temp;
        }

        return CollectionUtils.toString(arr);
    }


}